package com.enba.rbacadmin.module.dict.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.enba.boot.core.base.Result;
import com.enba.rbacadmin.annotation.DistributedLock;
import com.enba.rbacadmin.module.dict.controller.req.DictPageReq;
import com.enba.rbacadmin.module.dict.controller.req.SysDictAddUpdateReq;
import com.enba.rbacadmin.module.dict.controller.req.SysDictItemAddUpdateReq;
import com.enba.rbacadmin.module.dict.controller.resp.SysDictItemPageResp;
import com.enba.rbacadmin.module.dict.controller.resp.SysDictPageResp;
import com.enba.rbacadmin.module.dict.entity.SysDict;
import com.enba.rbacadmin.module.dict.entity.SysDictItem;
import com.enba.rbacadmin.module.dict.service.SysDictService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dict")
public class SysDictController {

  @Autowired private SysDictService sysDictService;

  @DistributedLock
  @PostMapping
  public Result<Boolean> saveOrUpdate(@RequestBody @Validated SysDictAddUpdateReq req) {
    return Result.success(sysDictService.saveOrUpdate(req));
  }

  @DeleteMapping("/{id}")
  public Result<Boolean> delete(@PathVariable Long id) {
    return Result.success(sysDictService.delete(id));
  }

  @GetMapping("/{id}")
  public Result<SysDict> getById(@PathVariable Long id) {
    return Result.success(sysDictService.getById(id));
  }

  @PutMapping("/status")
  public Result<Boolean> updateStatus(@RequestBody SysDict sysDict) {
    return Result.success(sysDictService.updateById(sysDict));
  }

  @DeleteMapping("/batch")
  public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
    return Result.success(sysDictService.removeByIds(ids));
  }

  @PostMapping("/page")
  public Result<List<SysDictPageResp>> page(@RequestBody @Validated DictPageReq req) {
    Page<SysDict> sysDictPage =
        sysDictService.listByCondition(
            new Page<>(req.getPageNum(), req.getPageSize()), req.getDictName(), req.getStatus());

    return Result.pageSuccess(
        BeanUtil.copyToList(sysDictPage.getRecords(), SysDictPageResp.class),
        sysDictPage.getTotal());
  }

  /**
   * 字典项列表查询
   *
   * @param req
   * @return
   */
  @PostMapping("/item-page/{dictId}")
  public Result<List<SysDictItemPageResp>> itemPage(
      @PathVariable Long dictId, @RequestBody @Validated DictPageReq req) {
    Page<SysDictItem> sysDictPage =
        sysDictService.listItemByCondition(
            new Page<>(req.getPageNum(), req.getPageSize()),
            dictId,
            req.getDictName(),
            req.getStatus());

    return Result.pageSuccess(
        BeanUtil.copyToList(sysDictPage.getRecords(), SysDictItemPageResp.class),
        sysDictPage.getTotal());
  }

  /** 字典项新增或编辑 */
  @DistributedLock
  @PostMapping("/item")
  public Result<Boolean> itemSaveOrUpdate(@RequestBody @Validated SysDictItemAddUpdateReq req) {
    return Result.success(sysDictService.saveOrUpdate(req));
  }

  /** 删除字典项 */
  @DeleteMapping("/item/{id}")
  public Result<Boolean> itemDelete(@PathVariable Long id) {
    return Result.success(
        sysDictService
                .getSysDictItemMapper()
                .delete(Wrappers.<SysDictItem>lambdaQuery().eq(SysDictItem::getId, id))
            > 0);
  }
}
